package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Admin;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.QiniuUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {
    public static final String SUCCESS_PAGE="common/successPage";
    @Reference
    private AdminService adminService;
    @Reference
    private RoleService roleService;
//    注入密码加密器
    @Autowired
    private PasswordEncoder passwordEncoder;
// 分页及带条件的查询
    @RequestMapping
    public String findPage(Map map, HttpServletRequest request){
        Map<String, Object> filters = getFilters(request);
        PageInfo<Admin> pageInfo = adminService.findPage(filters);
        map.put("filters",filters);
        map.put("page",pageInfo);
        return "admin/index";
    }
//    去添加用户的页面
    @RequestMapping("/create")
    public String goAddPage(){
        return "admin/create";
    }
//    添加用户
    @RequestMapping("/save")
    public String save(Admin admin){
//        密码加密
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminService.insert(admin);
        return SUCCESS_PAGE;
    }
//     删除用户edit
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id){
        adminService.delete(id);
        return "redirect:/admin";
    }
//    去修改用户的页面
    @RequestMapping("/edit/{id}")
    public String toEditPage(@PathVariable("id") Long id,Map map){
        Admin admin = adminService.getById(id);
        map.put("admin",admin);
        return "admin/edit";
    }
//    修改用户
    @RequestMapping("/update")
    public String update(Admin admin){
        adminService.update(admin);
        return SUCCESS_PAGE;
    }
//    去上传头像界面
    @RequestMapping("/uploadShow/{id}")
    public String goUploadPage(@PathVariable("id")Long id,Map map){
        map.put("id",id);
        return "admin/upload";
    }
//    上传头像
    @RequestMapping("upload/{id}")
    public String upload(@PathVariable("id")Long id, MultipartFile file){
        try {
            Admin admin = adminService.getById(id);
            byte[] bytes = file.getBytes();
            String fileName = UUID.randomUUID().toString();
            QiniuUtils.upload2Qiniu(bytes,fileName);
            admin.setHeadUrl("http://rkq8tmhrb.hn-bkt.clouddn.com/"+fileName);
            adminService.update(admin);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "common/successPage";
    }
//    去分配角色页面
    @RequestMapping("/assignShow/{adminId}")
    public String goAssignShow(@PathVariable("adminId") Long adminId, ModelMap modelMap){
//      将用户id放到request域中
        modelMap.addAttribute("adminId",adminId);
//      调用根据用户id查询用户角色的方法
        Map<String, Object> rolesByAdminId = roleService.findRolesByAdminId(adminId);
//        将map放到request域中
        modelMap.addAllAttributes(rolesByAdminId);
        return "admin/assignShow";
    }
//    分配角色
    @RequestMapping("/assignRole")
    public String assignRole(Long adminId,Long[] roleIds){
        roleService.assignRole(adminId,roleIds);
        return "common/successPage";
    }
}
